var express = require('express');
var request = require('request');
var app = new express();
app.use(express.static('public'));
app.get('/', function(req, res, next) {
	res.json(req.headers);
});
var baseUrl = 'http://news-at.zhihu.com';
//反向代理
app.get(/^\/api\/.+$/, function(req, res, next) {
	request.get(baseUrl + 　req.url, function(error, response, body) {
		if (error) {
			console.log('request error', error);
			res.json({});
		} else {
			res.send(body);
		}
	});
});
app.listen('3000', function() {
	console.log('app.js + zepto.js ready port 3000');
});

//图片防盗链
app.get('/img/proxy', function(req, res, next) {
	if (!req.query.img) {
		res.status(400);
		res.send('Bad Request');
	}

	request.get({
		url: req.query.img,
		headers: {
			referrer: 'http://news-at.zhihu.com/'
		}
	}).pipe(res);
});
module.exports = app;

